package ControleDiversas;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import ModeloAcademico.Alunos;




public class AlunoDao {
	private Alunos alunos;
	private Connection   conexao;
	 
	
	public AlunoDao(){
		alunos = new Alunos();
		conexao = new JBCD.Conexao().getConnection();
	}
	
	public void adicionaAluno(Alunos a) throws SQLException{
		String sql = "INSERT INTO aluno (nome,endereco,email,datanascimento,cpf,rg) values (?,?,?,?,?,?)";
		
		PreparedStatement stmt = conexao.prepareStatement(sql);
		stmt.setString(1, a.getNome());
		stmt.setString(2, a.getEndereco());
		stmt.setString(3, a.getEmail());
		stmt.setString(4, a.getDataNascimento());
		stmt.setString(5, a.getCpf());
		stmt.setString(6, a.getRg());
		stmt.execute();
		stmt.close();
		
	}

	public void editaContato(Alunos a) throws SQLException{
		
		String sql = "UPDATE aluno set  nome=? ,endereco=?,email=?,datanascimento=?,cpf=?,rg=? where id = ?  ";
		PreparedStatement stmt = conexao.prepareStatement(sql);
		stmt.setLong(7, (new Integer(a.getId())));
		stmt.setString(1, a.getNome());
		stmt.setString(2, a.getEndereco());
		stmt.setString(3, a.getEmail());
		stmt.setString(4, a.getDataNascimento());
		stmt.setString(5, a.getCpf());
		stmt.setString(6, a.getRg());
		stmt.execute();
		stmt.close();
		
		
	}
	//---------------------------------------------------------------------------------------------
	public List<Alunos> pesquisar() throws SQLException {
		List<Alunos> alunos = new ArrayList<Alunos>();
		
			String sql = "SELECT * FROM aluno";
			PreparedStatement stmt = conexao.prepareStatement(sql);

            ResultSet set = stmt.executeQuery();
			while (set.next()) {
				Alunos instituicao = new Alunos();
				instituicao.setId(Integer.valueOf(set.getString("id")));
				instituicao.setNome(set.getString("nome"));
				instituicao.setEndereco(set.getString("endereco"));
				instituicao.setEmail(set.getString("email"));
				instituicao.setDataNascimento(set.getString("datanascimento"));
				instituicao.setCpf(set.getString("cpf"));
				instituicao.setRg(set.getString("rg"));
				alunos.add(instituicao);
			}
			return alunos;
		}
			
}
